package com.ml.doctor.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.ml.doctor.R;
import com.ml.doctor.network.NetworkApi;
import com.ml.doctor.network.NetworkManager;
import com.ml.doctor.utils.StringUtils;
import com.ml.doctor.utils.ToastUtil;
import com.ml.doctor.view.CountDownTextView;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ResActivity extends BaseActivity {


    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.ct_get_code)
    CountDownTextView ctGetCode;
    @BindView(R.id.et_password1)
    EditText etPassword1;
    @BindView(R.id.et_password2)
    EditText etPassword2;
    String code;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_res);
        ButterKnife.bind(this);
        setTopTitle("注册");
    }


    @OnClick({R.id.ct_get_code, R.id.login})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ct_get_code:
                getCode();
                break;
            case R.id.login:
                checkRes();
                break;
        }
    }

    private void getCode() {
        //显示加载dialog
        if (TextUtils.isEmpty(etPhone.getText().toString())) {
            ToastUtil.showShort(ResActivity.this, "手机号不能为空");
            return;
        }
        if (!StringUtils.isPhone(etPhone.getText().toString())) {
            ToastUtil.showShort(ResActivity.this, "请输入正确的手机号");
            return;
        }
        showLoadingDialog("验证码获取中");
        Map<String, String> map = new HashMap<>();
        map.put("mobile", etPhone.getText().toString());
        NetworkManager.getInstance().getResultClass(NetworkApi.BasicUrl + "docter/GainCode", map, String.class, new NetworkManager.SuccessCallback<String>() {
            @Override
            public void onSuccess(String response) {
                code = response;
                ctGetCode.start(60);
                hideLoadingDialog();
            }
        }, new NetworkManager.FailedCallback() {
            @Override
            public void onFailed(String message) {
                ToastUtil.showShort(ResActivity.this, message);
                hideLoadingDialog();
            }
        });
    }

    private void checkRes() {
        if (TextUtils.isEmpty(etPassword1.getText().toString()) || TextUtils.isEmpty(etPassword2.getText().toString())) {
            ToastUtil.showShort(ResActivity.this, "请输入密码");
            return;
        }
        if (!etPassword1.getText().toString().equals(etPassword2.getText().toString())) {
            ToastUtil.showShort(ResActivity.this, "两次输入的密码不一致");
            return;
        }
        if (TextUtils.isEmpty(code)) {
            ToastUtil.showShort(ResActivity.this, "请获取验证码");
            return;
        }
        if (TextUtils.isEmpty(etCode.getText().toString())) {
            ToastUtil.showShort(ResActivity.this, "请输入验证码");
            return;
        }
        if (!etCode.getText().toString().equals(code)) {
            ToastUtil.showShort(ResActivity.this, "验证码不正确");
            return;
        }
        Intent i = new Intent(this, UserInfoSetActivity.class);
        i.putExtra("password", etPassword1.getText().toString());
        i.putExtra("phone", etPhone.getText().toString());
        startActivity(i);
    }
}
